﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using System.Data;
using Jlob.OpenErpNet;
using CookComputing.XmlRpc;

namespace Jlob.OpenErpNet.EFProvider
{
    public sealed class OpenErpConnection:DbConnection
    {

        private OpenErpConnectionString connectionString;
        private ConnectionState state;
        private IOpenErp OpenErpClient { get; set; }
        private IOpenErpLogin OpenErpLogin { get; set; }
        private const string UrlLogin = "/xmlrpc/common";
        private const string UrlClient = "/xmlrpc/object";

        protected override DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
        {
            throw new NotImplementedException();
        }

        public override void ChangeDatabase(string databaseName)
        {
            this.Close();
            this.connectionString.InitialCatalog = databaseName;
        }

        public override void Close()
        {
            this.state = ConnectionState.Closed;
            this.OpenErpClient = null;
            this.OpenErpLogin = null;
        }

        public override string ConnectionString
        {
            get
            {
                if (connectionString != null)
                {
                    return connectionString.Text;
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                connectionString = new OpenErpConnectionString(value);
            }
        }

        protected override DbCommand CreateDbCommand()
        {
            return new OpenErpCommand(this);
        }

        public override string DataSource
        {
            get {
                if (connectionString != null)
                {
                    return connectionString.DataSource;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public override string Database
        {
            get {
                if (connectionString != null)
                {
                    return connectionString.InitialCatalog;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public override void Open()
        {
            if (this.state == ConnectionState.Closed)
            {
                if (this.OpenErpLogin == null)
                {
                    this.OpenErpLogin = XmlRpcProxyGen.Create<IOpenErpLogin>();
                    this.OpenErpClient = XmlRpcProxyGen.Create<IOpenErp>();
                }
                this.OpenErpClient.Url = this.DataSource + UrlClient;
                this.OpenErpLogin.Url = this.DataSource + UrlLogin;
            }
            try
            {
                this.OpenErpLogin.login(this.Database, this.connectionString.UserId, this.connectionString.Password);
                this.state = ConnectionState.Open;
            }
            catch (XmlRpcServerException xmlEx)
            {
                if (xmlEx.Message.Equals("NOT FOUND"))
                {
                    throw new System.Net.WebException("Incorrect Data Source url");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override string ServerVersion
        {
            get { return "7.0"; }
        }

        public override System.Data.ConnectionState State
        {
            get { return this.state; }
        }

    }
}
